﻿using System;

namespace Project1
{
	/// <summary>
	/// A Manhattan distance heuristic implementation.
	/// </summary>
	/// <remarks>This implementation can be used for any number of dimensions.</remarks>
	public class ManhattanDistanceHeuristic : IHeuristic
	{
		private int ManhattanDistance(IPoint a, IPoint b)
		{
			//
			// TODO: Add vector size validation, have to be of equal size.
			//
			int[] vectorA = a.GetDimensionalVector();
			int[] vectorB = b.GetDimensionalVector();
			//
			// Sum up the distances for each dimension and return
			//
			int distance = 0;
			for (int index = 0; index < vectorA.Length; ++index)
			{
				distance += Math.Abs(vectorA[index] - vectorB[index]);
			}
			return distance;
		}

		#region IHeuristic Members

		/// <summary>
		/// Estimates the cost from the supplied <paramref name="state"/> to the goal.
		/// </summary>
		/// <param name="problem">The problem.</param>
		/// <param name="state">The state.</param>
		/// <returns>
		/// The estimated cost
		/// </returns>
		int IHeuristic.EstimateCost(IProblem problem, State state)
		{
			State goalState = problem.GoalState;

			IPoint goalPoint = (IPoint)goalState.Key;
			IPoint statePoint = (IPoint)state.Key;

			return ManhattanDistance(goalPoint, statePoint);
		}

		#endregion
	}
}
